﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TMDT_Nhom1A.DTO;
using System.Collections.Generic;

namespace TMDT_Nhom1A.DAO
{
    public class AccountTransportWebserviceDAO
    {
        private static QL_DoanhNghiepDataContext data = new QL_DoanhNghiepDataContext();

        public static List<AccountTransportWebserviceDTO> getAccountTransportWS(int transportId)
        {
            try
            {
                List<AccountTransportWebserviceDTO> accList = new List<AccountTransportWebserviceDTO>();
                var accountResult = from acc in data.AccountTransportWebservices
                                                            join transWS in data.TRANSPORT_WEBSERVICEs on acc.TransportWS_ID equals transWS.TRANSPORT_WS_ID
                                                            where acc.TransportWS_ID == transportId
                                                            select new 
                                                            {
                                                                Id = acc.ID,
                                                                username = acc.Username,
                                                                password = acc.Password,
                                                                transportName = transWS.TransportCompanyName
                                                            };
                if(accountResult.Count() == 1)
                {
                    foreach (var accountItem in accountResult)
	                {
                        AccountTransportWebserviceDTO account = new AccountTransportWebserviceDTO();
                        account.Id = accountItem.Id;
                        account.Username = accountItem.username;
                        account.Password = accountItem.password;
                        account.TransportName = accountItem.transportName;
                        accList.Add(account);
	                }
                }
                return accList;

            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static bool insertAccountTransportWS(AccountTransportWebserviceDTO account)
        {
            try
            {
                

                AccountTransportWebservice accountTransportWS = new AccountTransportWebservice();
                accountTransportWS.Username = account.Username;
                accountTransportWS.Password = account.Password;
                accountTransportWS.TransportWS_ID = account.TransportId;

                data.AccountTransportWebservices.InsertOnSubmit(accountTransportWS);
                data.SubmitChanges();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static bool updateAccountTransportWS(AccountTransportWebserviceDTO account)
        {
            try
            {
                AccountTransportWebservice accountTransport = (from acc in data.AccountTransportWebservices
                                                               where acc.ID == account.Id
                                                               select acc).Single();
                if (accountTransport != null)
                {

                    accountTransport.Username = account.Username;
                    accountTransport.Password = account.Password;
                    accountTransport.TransportWS_ID = account.TransportId;
                    data.SubmitChanges();
                    return true;
                }
                return false;
                
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static bool deleteAccountTransportWS(int id)
        {
            try
            {
                AccountTransportWebservice accountTransport = (from acc in data.AccountTransportWebservices
                                                               where acc.ID == id
                                                               select acc).Single();
                if (accountTransport != null)
                {

                    data.AccountTransportWebservices.DeleteOnSubmit(accountTransport);
                    data.SubmitChanges();
                    return true;
                }
                return false;

            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
